package com.auctionhaus

import grails.test.*

class ListingControllerTests extends ControllerUnitTestCase {
    transient Date testFutureDate =  new Date()+1;
    transient Date testCurrentDate =  new Date();

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    // This checks out
    void testIndex(){
        controller.index()
        assertEquals "list", redirectArgs.action
    }

    // This checks out
    void testCanCreateListing(){

        mockRequest.method = 'POST'

        def seller = new Customer()
        seller.emailAddress = "GenericEmail@gmail.com"
        seller.password = "Password"
        seller.date = testCurrentDate

        mockDomain(Customer,[seller])
        mockDomain(Listing)

        // create the customer params
        mockParams.creationDate = testCurrentDate
        mockParams.listName = "2011 Jeep Grand Cherokee"
        mockParams.endDateTime = testFutureDate
        mockParams.startBidPrice = "15000"
        mockParams.seller = seller

        // verify that we have one customer
        def model = controller.create()
        assertEquals 1, model.size()

        assert model?.listingInstance
        def listing = model.listingInstance

        // make sure we have no errors
        assertFalse 'no errors found', listing.hasErrors()
    }

    void testSaveListing(){

        mockRequest.method = 'POST'

        def seller = new Customer(emailAddress:"GenericEmail@gmail.com",
                                  password:"Password",
                                  date: testCurrentDate);
        mockDomain(Customer,[seller])
        mockDomain(Listing)
        mockParams.creationDate = testCurrentDate
        mockParams.listName = "2011 Jeep Grand Cherokee"
        mockParams.endDateTime = testFutureDate
        mockParams.startBidPrice = 15000
        mockParams.seller = seller

        // verify that we have one Listing
        controller.metaClass.message = {args -> println "listing saved: ${args}"}
        def listingModel = controller.save()
        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        assertEquals('show', redirectArgs.action)
        assertEquals(myListing.id, redirectArgs.id)
    }

}
